#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "key.h"


struct sKey
{
	char str[KEY_MAX_LEN];
	int tam;
};

int
key_maxLen(void)
{
return KEY_MAX_LEN;
}

Key
key_fromString(char *s)
{
	Key k;
	int n;
	n = key_maxLen();
	k = (Key) malloc (sizeof(struct sKey));
	if (k!=NULL)
	{
		strncpy(k->str,s,n);
		k->tam = strnlen(k->str,n);
	}
	else
		exit(EXIT_FAILURE);

return k;
}

void
key_toString(Key k, char *s)
{
	int n;
	n = key_maxLen();	
	strncpy(s,k->str,n);
}

int
key_length(Key k)
{
return (k->tam);
}

Bool
key_eq(Key k1, Key k2)
{
	Bool b;
	int n;
	n = key_maxLen();
	b = FALSE;
	if (strncmp(k1->str,k2->str,n) == 0)
		b = TRUE;

return b;
}

Bool
key_le(Key k1, Key k2)
{
	Bool b;
	int n;
	n = key_maxLen();
	b = FALSE;
	if (strncmp(k1->str,k2->str,n) < 0)
		b = TRUE;

return b;
}

Key
key_clone(Key k)
{
return(key_fromString(k->str));
}

Key
key_destroy(Key k)
{
	free(k);
	k = NULL;
return k;
}


